package com.ship.service.impl;

import com.ship.entity.bo.Users;
import com.ship.mapper.UserMapper;
import com.ship.service.UsersService;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UsersServiceImpl implements UsersService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Users queryById(String userId) {
        Users users = new Users();
        users.setId(userId);
        Users us = userMapper.selectOne(users);
        return us;
    }

    @Override
    public int editPwd(String userId, String oldPwd, String newPwd) {
        Users users = this.queryById(userId);
        if(users==null){
            return 1 ;//用户不存在
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String o = encoder.encode(oldPwd);
        if(!encoder.matches(oldPwd,users.getPassword())){
            return 2;//旧密码错误
        }
        encoder = new BCryptPasswordEncoder();
        users.setPassword(encoder.encode(newPwd));
        userMapper.updateByPrimaryKeySelective(users);
        return 0;
    }
}
